package tables

import (
	"encoding/json"
	"gorm.io/gorm"
	"reflect"
	"time"
)

type Model struct {
	ID        uint           `gorm:"primarykey" json:"id"`
	CreatedAt time.Time      `json:"created_at" format:"2006-01-02 15:04:05"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}

func MarshalJson(m json.Marshaler) ([]byte, error) {
	mValue := reflect.ValueOf(m)
	createdAt := mValue.FieldByName("CreatedAt").Interface().(time.Time)
	updatedAt := mValue.FieldByName("UpdatedAt").Interface().(time.Time)
	aux := struct {
		json.Marshaler
		CreateAt string `json:"created_at"`
		UpdateAt string `json:"updated_at"`
	}{
		Marshaler: m,
		CreateAt:  createdAt.Format("2006-01-02 15:04:05"),
		UpdateAt:  updatedAt.Format("2006-01-02 15:04:05"),
	}

	return json.Marshal(&aux)
}
